package com.zhongxing.hdy.spritel_hdy;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.zhongxing.hdy.spritel_hdy.adapter.SubViewAdapter;
import com.zhongxing.hdy.spritel_hdy.util.AnimUtil;
import com.zhongxing.hdy.spritel_hdy.view.ShopView;
import com.zhongxing.hdy.spritel_hdy.view.ShouYeView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity{
    private ImageView iv_message;
    private ImageView iv_shouye;
    private ImageView iv_shop;
    private FrameLayout fl_beibao;
    private FrameLayout fl_fold;
    private LinearLayout ll_sub_btns;
    private ViewPager vp_content;
    private RelativeLayout rl_root;

    private ProgressBar pb_timer_l;

    private List<View> subViewList;

    private ShouYeView shouYeView;
    private ShopView shopView;

    private List<ImageView> imageViewMainList=new ArrayList<>();//主菜单按钮列表
    private List<FrameLayout> imageViewSubList=new ArrayList<>();//上一层菜单按钮列表

    @Override
    //Activity默认的启动方法，自动运行
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//指定Activity显示的布局文件

        initUI();
        updateTimer();
        initSubViewList();
    }

    private void updateTimer(){
        new Thread(){
            @Override
            public void run() {
                while (true){
                    int progress =  pb_timer_l.getProgress();
                    if (progress == 100){
                        progress = -1;
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    pb_timer_l.setProgress(progress + 1);
                }

            }
        }.start();
    }

    private void initSubViewList(){
        subViewList = new ArrayList<View>();
        shouYeView = new ShouYeView(this);
        shopView = new ShopView(this);

        subViewList.add(shouYeView.getView());
        subViewList.add(shopView.getView());

        SubViewAdapter adapter = new SubViewAdapter(getApplicationContext(), subViewList);
        vp_content.setAdapter(adapter);
    }

    private void initUI(){
        List<Integer> idMainList = new ArrayList<>();
        idMainList.add(R.id.btn_home);
        idMainList.add(R.id.btn_level);
        idMainList.add(R.id.btn_raid);
        idMainList.add(R.id.btn_shop);
        idMainList.add(R.id.btn_friends);
        idMainList.add(R.id.btn_setting);

        for (int id : idMainList) {
            imageViewMainList.add((ImageView) findViewById(id));
        }
        for (ImageView mainView : imageViewMainList) {
            AnimUtil.addOnTouchListener(mainView, 0.5f, 0.75f, null);
        }
        idMainList.clear();
        //上一层
        idMainList.add(R.id.btn_backpack);
        idMainList.add(R.id.btn_rongyu);
        idMainList.add(R.id.btn_liuyan);
        idMainList.add(R.id.btn_renwu);
        idMainList.add(R.id.btn_chuzhan);
        idMainList.add(R.id.btn_hecheng);
        for (int id : idMainList) {
            imageViewSubList.add((FrameLayout) findViewById(id));
        }
        for (FrameLayout mainView : imageViewSubList) {
            AnimUtil.addOnTouchListener(mainView, 0.5f, 0.75f, null);
        }
        ///////////////////////////////////
        rl_root = (RelativeLayout) findViewById(R.id.rl_root);
        vp_content = (ViewPager) findViewById(R.id.vp_content);
        //禁止滑动
        vp_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        iv_message = (ImageView) findViewById(R.id.btn_message);
        AnimUtil.addOnTouchListener(iv_message, 0f, 0f, null);

        iv_shouye = (ImageView) findViewById(R.id.btn_home);
        AnimUtil.addOnTouchListener(iv_shouye, 0.5f, 1f, animListener);

        iv_shop = (ImageView) findViewById(R.id.btn_shop);
        AnimUtil.addOnTouchListener(iv_shop, 0.5f, 1f, animListener);

        fl_beibao = (FrameLayout) findViewById(R.id.btn_backpack);
        AnimUtil.addOnTouchListener(fl_beibao, 0.5f, 1f, null);

        fl_fold = (FrameLayout) findViewById(R.id.fl_fold);
        String[] proNames = new String[]{AnimUtil.PROPERTY_SCALEX, AnimUtil.PROPERTY_SCALEY};
        AnimUtil.addOnTouchListener(fl_fold, proNames,
                new float[]{1f, 0.75f}, new float[]{0.75f, 1f}, animListener);

        ll_sub_btns = (LinearLayout) findViewById(R.id.ll_sub_btns);

        pb_timer_l = (ProgressBar) findViewById(R.id.pb_timer_l);

    }

    private AnimUtil.AnimListener animListener = new AnimUtil.AnimListener() {
        boolean isDown;

        @Override
        public void down(View v) {

        }

        @Override
        public void move(View v) {

        }

        @Override
        public void up(View v) {
            switch (v.getId()){
                case R.id.btn_shop:
                    Log.i("MainActivity", "shop");
                    if (vp_content.getCurrentItem() != 1) {
                        vp_content.setCurrentItem(1);
                        rl_root.setBackgroundResource(R.mipmap.bg_shop);
                        shopView.show();
                    }
                    break;
                case R.id.btn_home:
                    Log.i("MainActivity", "shouye");
                    //
                    if (vp_content.getCurrentItem() != 0) {
                        addAnimationListener(iv_shouye);
                    }
//                    vp_content.setCurrentItem(0);
//                    rl_root.setBackgroundResource(R.mipmap.background);
                    break;
                case R.id.fl_fold:
                    int subHeight = ll_sub_btns.getHeight() - 32;
                    if (isDown){
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 180f, 360f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, 0f);
                    }else {
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, subHeight);
                        AnimUtil.addObjectAnimation(v, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY},  0f, 180f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, subHeight);
                    }
                    isDown = !isDown;
                    break;
            }
        }
    };

    public void addAnimationListener(final View view) {
        shopView.hide(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                switch (view.getId()) {
                    case R.id.btn_home:
                        vp_content.setCurrentItem(0);
                        rl_root.setBackgroundResource(R.mipmap.background);
                        break;
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}